package b2.widget.warehouse
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.player.dataModel.Mail;
	import b2.player.dataModel.Obj;
	import b2.player.dataModel.ObjWithNum;
	import b2.scene.*;
	import b2.scene.guide.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.sendMail.SendGiftDialog;
	import b2.widget.sendMail.SendMailTab;
	import b2.widget.tab.*;
	import b2.widget.tank.*;
	import b2.widget.tech.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.text.*;
	import flash.ui.Mouse;
	
	public class ObjectItem extends Sprite
	{
		//物品索引
		public var objectIndex:int;
		//物品基础等级
		public var basicLevel:int;
		//物品强化等级
		public var strengthenLevel:int;
		//数量
		private var objectNum:int;
		//拖拽前的父容器
		private var oldParent:DisplayObjectContainer;
		//拖拽前的位置x
		private var oldPosX:Number;
		//拖拽前的位置y
		private var oldPosY:Number;
		
		private var tankId:int;
		
		//-1, 无位置区分（不能被双击，点击，或者拖拽）; 0, 仓库; 1, 科技塔; 2, 坦克界面
		public var location:int;
		
		public function ObjectItem(objectIndex:int, basicLevel:int, strengthenLevel:int, tankId:int, objectNum:int, location:int = -1,showNumFlag:Boolean = true)
		{
			super();
			this.objectIndex = objectIndex; 
			this.basicLevel = basicLevel;
			this.strengthenLevel = strengthenLevel;
			this.tankId = tankId;
			this.objectNum = objectNum;
			this.location = location;

			var objectConfig:CommonObjConfig = Config.objectConfigs[this.objectIndex];
			//物品图片
			var ObjectImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage);
			var objectSprite:Sprite = new ObjectImage();
			if (!objectConfig.noBasicLevel)
			{
				//背景框
				var ObjectItemBgr:Class = ResourceHelper.getInstance().getClassByName("ObjectItemBgr" + basicLevel);
				var objectItemBgr:Sprite = new ObjectItemBgr();
				objectItemBgr.width = 50;
				objectItemBgr.height = 50;
				addChild(objectItemBgr);
//				//环绕发光
//				var CircleLight:Class = ResourceHelper.getInstance().getClassByName("CircleLight");
//				var circleLight:Sprite = new CircleLight();
//				circleLight.width = 36;
//				circleLight.height = 53;
//				addChild(circleLight);
				//齐天大圣
				if (objectIndex == 10015)
				{
					objectSprite.width = 63.25;
					objectSprite.height = 71;
				}
				//死亡骑士
				else if (objectIndex == 10020)
				{
					objectSprite.width = 55;
					objectSprite.height = 55;
				}
				else
				{
					objectSprite.width = 46;
					objectSprite.height = 46;
				}
				objectSprite.x = 2;
				objectSprite.y = 2;
			}
			else
			{
				objectSprite.width = 50;
				objectSprite.height = 50;
			}
			addChild(objectSprite);
			if (!objectConfig.noBasicLevel && !objectConfig.noStrengthenLevel)
			{ 
				//显示星级
				var levelText:TextField = new TextField();
				levelText.selectable = false;
	        	levelText.autoSize = TextFieldAutoSize.LEFT;
				levelText.defaultTextFormat = new TextFormat("微软雅黑", 11, 0xFFFFFF);
				levelText.text = Config.basicConfig.objectStrengthenLevel[this.strengthenLevel];
				levelText.x = (50 - levelText.width) - 1;
				levelText.y = 0;
				addChild(levelText);
			}
			//显示数量
			if (showNumFlag)
			{
				var numText:TextField = new TextField();
				numText.selectable = false;
        		numText.autoSize = TextFieldAutoSize.LEFT;
				numText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000);
				numText.filters = [new GlowFilter(0xFFFFFF, 1, 1.2, 1.2, 100, BitmapFilterQuality.HIGH)];
				if (objectNum < 1000000)
					numText.text = objectNum.toString();
				else
					numText.text = int(objectNum / 10000).toString() + "w";
				numText.x = (50 - numText.width) - 2;
				numText.y = 32;
				addChild(numText);
			}
			
			this.mouseChildren = false;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			if (location != -1)
			{
				this.doubleClickEnabled = true;
				addEventListener(MouseEvent.DOUBLE_CLICK, onMouseDoubleClick);
				addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			}
		}
		
		protected function onMouseDoubleClick(event:MouseEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var objectConfig:CommonObjConfig = Config.objectConfigs[this.objectIndex];
			var equipConfig:EquipConfig;
			var comIndex:String = objectIndex + "_" + basicLevel + "_" + strengthenLevel + "_" + tankId;
			 
			//坦克装备
			if (TankDialog.getInstance().parent != null && objectConfig.type == 1)
			{
				var targetTankId:int = TankDialog.getInstance().tankSlot.tankId;
				var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[targetTankId];
				equipConfig = objectConfig as EquipConfig;
				//装备
				if (this.tankId == 0)
				{
					if (TankConfig.EQUIP_SLOT_AVAILABLE[equipConfig.position] <= tankInfo.basicLevel)
					{
						var originalEquipIndex:String = tankInfo.getEquipByPosition(equipConfig.position);
						var newComIndex:String = objectIndex + "_" + basicLevel + "_" + strengthenLevel + "_" + targetTankId;
						ServerHelper.getInstance().changeEquip(newComIndex, originalEquipIndex);
						ServerHelper.getInstance().addEventListener(ServerEvent.CHANGE_EQUIP, onChangeEquipComplete);
					}
					else
					{
						var popMessage:PopMessage = new PopMessage("装备失败，此坦克槽位尚未开启", 0xFF0000);
						WidgetManager.getInstance().popOut([popMessage], false, true);
					}
				}
				//卸载
				else if (this.tankId == targetTankId)
				{
					equipConfig = objectConfig as EquipConfig;
					if (equipConfig.position != 5 && equipConfig.position != 9)
					{
						originalEquipIndex = objectIndex + "_" + basicLevel + "_" + strengthenLevel + "_" + targetTankId;
						ServerHelper.getInstance().changeEquip(null, originalEquipIndex);
						ServerHelper.getInstance().addEventListener(ServerEvent.CHANGE_EQUIP, onChangeEquipComplete);
					}
				}
			}
			//科技塔
			else if (TechTower.getInstance().parent != null)
			{
				var techTab:TechTab = TechTower.getInstance().tabs[TechTower.getInstance().currentTabIndex] as TechTab;
				if (this.location == 0)
				{
					techTab.loadObject(objectIndex, basicLevel, strengthenLevel);
				}
				else if (this.location == 1)
				{
					techTab.removeAll();
				}
				Warehouse.getInstance().refresh();
			}
			else if (SendMailTab.getInstance().parent != null)
			{
				var mail:Mail = SendMailTab.getInstance().mail;
				var sendMailTab:SendMailTab = SendMailTab.getInstance();
				if (this.location == 0)
				{
					if (me.isAdmin || objectConfig.priceGas == 0)
					{
						var gift:ObjWithNum = new ObjWithNum(new Obj(objectIndex, basicLevel, strengthenLevel), me.getObjNum(objectIndex, basicLevel, strengthenLevel));
						var sendGiftDialog:SendGiftDialog = new SendGiftDialog(gift);
						WidgetManager.getInstance().popOut([sendGiftDialog], false, true);
					}
					else
					{
						popMessage = new PopMessage("该物品不能互相赠送！", 0xFF0000);
						WidgetManager.getInstance().popOut([popMessage], false, true);
					}
				}
				else if (this.location == 1)
				{
					var index:int;
					for (var i:int = 0; i < sendMailTab.objectSlots.length; i++)
					{
						if ((sendMailTab.objectSlots[i] as ObjectSlot).objectItem == this)
						{
							index = i;
							break;
						}
					}
					mail.removeGift(index);
				}
			}
		}
		
		private function onChangeEquipComplete(event:ServerEvent):void
		{
			var targetTankId:int = TankDialog.getInstance().tankSlot.tankId;
			TankDialog.getInstance().fresh(targetTankId);
			Warehouse.getInstance().refresh();
			Warehouse.getInstance().selectTab(1);
			GuideScene.getInstance().step++;
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			//没有拖拽
			if (this.parent == SceneManager.getInstance().currentScene)
				return;
			var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
			var itemTip:ObjectItemMessage = new ObjectItemMessage(this); 
			itemTip.x = globalPoint.x + this.width;
			itemTip.y = globalPoint.y;
			if (itemTip.x + itemTip.width > BFont.stageWidth)
				itemTip.x = globalPoint.x - itemTip.width;
			if (itemTip.y + itemTip.height > BFont.stageHeight)
				itemTip.y = BFont.stageHeight - itemTip.height;
			WidgetManager.getInstance().popOutMouseOverMessage(itemTip);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
			var mouseOverMessage:Sprite = WidgetManager.getInstance().mouseOverMessage;
			if (mouseOverMessage != null)
			{
				if (event.stageX < mouseOverMessage.x || event.stageX > mouseOverMessage.x + mouseOverMessage.width || 
					event.stageY < mouseOverMessage.y || event.stageY > mouseOverMessage.y + mouseOverMessage.height)
				{
					WidgetManager.getInstance().closeMouseOverMessage(); 
				}
			}
		}
		
		//开始拖拽
		private function onMouseDown(event:MouseEvent):void
		{
			//Fix bug: 新手引导时候拖拽和双击有冲突
			if (GuideScene.getInstance().enabled)
				return;
			if (this.parent != SceneManager.getInstance().currentScene)
			{ 
				var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
				this.oldPosX = this.x;
				this.oldPosY = this.y;
				this.oldParent = this.parent;
				this.x = globalPoint.x;
				this.y = globalPoint.y;
				SceneManager.getInstance().currentScene.addChild(this);
				WidgetManager.getInstance().closeMouseOverMessage();
				this.startDrag();
			}
		}
		
		//拖拽结束
		private function onMouseUp(event:MouseEvent):void
		{
			//Fix bug: 新手引导时候拖拽和双击有冲突
			if (GuideScene.getInstance().enabled)
				return;
			this.stopDrag();
			if (this.parent != null)
				this.parent.removeChild(this);
			var parentWidget:Sprite;
			if (this.location == 0)
			{
				parentWidget = Warehouse.getInstance();
			}
			else if (this.location == 1)
			{
				parentWidget = TechTower.getInstance();
			}
			else
			{
				parentWidget = TankDialog.getInstance();
			}
			//如果拖拽没有离开原来的父容器
			if (this.x >= parentWidget.x && this.x < parentWidget.x + parentWidget.width &&
				this.y >= parentWidget.y && this.y < parentWidget.y + parentWidget.height)
			{
				//还原原来的位置
				this.x = this.oldPosX;
				this.y = this.oldPosY;
				if (this.oldParent != null)
				{
					this.oldParent.addChild(this);
					this.oldParent = null;
				}
			}
			else
			{
				onMouseDoubleClick(event);
				Warehouse.getInstance().refresh();
			}
		}
	}
}